import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)

  // 计算属性
  const doubleCount = computed(() => {
    return count.value * 2
  })

  // 同步修改
  const updateCount = () => {
    count.value++
  }
  const updateCountPlus = (num: number) => {
    count.value += num
  }

  // 异步修改
  const asyncUpdateCount = () => {
    setTimeout(() => {
      count.value++
    }, 2000)
  }

  // 后续 和数据相关的属性，方法直接写到这个对象中即可
  // 返回了一个对象，对象中有属性和方法
  return {
    count, //等同于 count:count
    doubleCount: doubleCount,
    updateCount,
    updateCountPlus,
    asyncUpdateCount
  }
})
